<?php

namespace App\Http\Controllers;

use App\Models\Member;
use App\Models\Orders;
use Illuminate\Http\Request;
use MongoDB\Driver\Session;
use Yansongda\Pay\Pay;

class AlipayController extends Controller
{
    protected $config=[
        'app_id'=>'2016091700529473',//应用的appid
        'return_url'=>'http://myshop.com/return',//回调地址
        'ali_public_key'=>'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwLaJ4Hz9mKGgizHFbYfNQrtbaKgtbpwR5WhX3TmhLK7LTZTKX/RE8oHm6LbcBzjcY6ck/auyyiazPrS4BkrLW+BqooyTtanE1UuxNYXCO7P8cYYPSUSQ427N/oRKyVbvdeoJwBOAxbRzjBluoRB9tBMoiBPQRBa01dKMdCA3TlkgcRiFi9fNy7wCH3mE1uUxmGDISx2NzD8XDCf/ZKaYlx5C2SKnQZgJ3kUKKLZx35sP4jw0O1ButJMtOg3y3hQziluW8Yx6MM+5S80q9kn29ERxSu7iNwEE3ZRyyucRy7TdL6Be9E+FVBZ2xPUMoBg54d2IUawnzyPOTfFvyKI40wIDAQAB',
        'private_key'=>'MIIEpQIBAAKCAQEA2QkiJ+r+XL1mZkkTMfcKVNLAZmXpxBDY1wE7FS3PsoA0kr7p4CRYTSGzXTisucTy2OaEg+HUcWzgyFq4fRfUwVUofivBwljgMOzzGrvSNo72FLWPmA56CCn0fOcuzl/+rvddfya0hR/uNKeZFzNpJL03oFP3aFR1ZkCzBb/w9W3KRTlROzSROreSwaIF7Utguzy4fR7McNM5Qk8znb+pt4L0z6M1gbmRHNEGanOTNmajUSlQkAVZg0pBpdmuAgQuHtGn37gtVB+sYwj2+Q1mHdW4SqPwQCMBLK9vVhu8l4ZU0suNHtzvuHaxSEETknWF0vmDpLISF+jya28qISiixwIDAQABAoIBAQDVEX1huPbcbuhflG/uxntzCK/wFy8m1+85LLEnOqFDuU9jnV+XN7ckieg/pYsUmUBcJfqESqk4bU5EA6RI0qOA5axqKWT/+iamgYisfyWBgFofvJrrCZNN/npSQNeXrXAnr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HAI=',
        'log'=>[
            'file'=>'./logs/alipay.log',
            'level'=>'info',
            'type'=>'single',//也可设置为daily
            'max_file'=>30
        ],
        'http'=>[
            'timeout'=>5.0,
            'connect_timeout'=>5.0,
        ],
        'mode'=>'dev'
    ];

    //调起支付页面
    public function Alipay(Request $request){

        //获取购买商品的id和num数量
        $productsjson = $request->input('productsJson');

        $request->session()->put('products',$productsjson);


        $total = $request->input('total');//获取商品的总金额

        $order=[
            'out_trade_no'=>time(),
            'total_amount'=>$total,
            'subject'=>'mall商城支付宝测试',
        ];
        $alipay = Pay::alipay($this->config)->web($order);
        return $alipay;
    }

    //支付回调
    public function AlipayReturn(Request $request){
        $data = Pay::alipay($this->config)->verify();//验签成功才会回调，否则失败

        //获取购买商品的ID和数量num
        $products = Session::pull('products');//pull的用法是取出session的数据后立减删除

        //json数据转换成数组
        $products = json_decode($products,true);

        $username =$request->cookie('usernameCookie');
        //获取电话号码和地址
        $phone = '';
        $address = '';
        $userinfo = Member::where('username','=',$username)->get();
        foreach ($userinfo as $item){
            $phone = $item->phone;
            $address = $item->province.$item->area.$item->address.'';

        }

        //遍历数组获取商品的ID和数量num,并将订单信息插入数据库
        foreach ($products as $k=>$v){
                Orders::created([
                    'username'=>$username,
                    'products_id'=>$k,
                    'num'=>$v,
                    'phone'=>$phone,
                    'address'=>$address

                ]);


        }



        return redirect('/');
    }
}
